<!DOCTYPE html>
<html>
	<head th:include="/common/head :: head">
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit|ie-comp|ie-stand">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<meta http-equiv="Cache-Control" content="no-siteapp" />
	</head>
<body>
	<div class="x-body">
		<form class="layui-form" lay-filter="dis_form">
			<div class="layui-form-item">
				<label for="id" class="layui-form-label">机会编码:
				</label>
				<div class="layui-input-inline">
					<input type="text" id="id" name="id" style="border:0px;outline:none"
						 autocomplete="off" class="layui-input" readOnly="readOnly">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="chanceName" class="layui-form-label">机会名称:
				</label>
				<div class="layui-input-inline">
					<input type="text" id="chanceName" name="chanceName" readOnly style="border:0px;outline:none"
						 autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="type" class="layui-form-label">机会类型:
				</label>
				<div class="layui-input-inline">
					<input type="text" id="type" name="type" readOnly style="border:0px"
						 autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="createBy" class="layui-form-label">录入人:
				</label>
				<div class="layui-input-inline">
					<input type="text" id="createBy" name="createBy" readOnly style="border:0px"
						 autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="createDate" class="layui-form-label">录入时间:
				</label>
				<div class="layui-input-inline">
					<input type="text" id="createDate" name="createDate" readOnly style="border:0px"
						 autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="updateBy" class="layui-form-label">最后操作人:
				</label>
				<div class="layui-input-inline">
					<input type="text" id="updateBy" name="updateBy" readOnly style="border:0px"
						 autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="updateDate" class="layui-form-label">最后修改时间:
				</label>
				<div class="layui-input-inline">
					<input type="text" id="updateDate" name="updateDate" readOnly style="border:0px"
						 autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label for="content" class="layui-form-label">机会内容:
				</label>
				<div class="layui-input-block">
                  <input type="text" id="content" name="content" autocomplete="off" readOnly class="layui-input" ></input>
                </div>
			</div>
			<hr>
			<div class="layui-form-item">
				<label for="userId" class="layui-form-label">分配给:
				</label>
				<div class="layui-input-inline">
					<select  id="userId" name="userId" lay-search="">
					</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label for="L_repass" class="layui-form-label"> </label>
				<button class="layui-btn" class="layui-btn" lay-filter="dis_form" lay-submit="">分配</button>
				<button class="layui-btn" onclick="black()">返回</button>
			</div>
		</form>
	</div>
</body>
</html>
<script>
	$(function(){
		//初始化
		init();
	})
	var _this={};
	var chanceId = sessionStorage.getItem("chanceId");
	//通过id拿到对象
	function init(){
		$.post("/chance/getChanceById","chanceId="+chanceId,function(result){
			if(result.status==0){
				//初始化修改表单（赋值）
				_this.chance=result.data;
				//获取用户姓名、id下拉表
				getUserId();
				//初始化赋值）
				initQuery(result.data);
			}else{
				layer.msg(result.msg);
			}
		})
	}
	//初始化（赋值）
	function initQuery(chance){
		layui.use(['form','layer'],function(){
			$=layui.jquery;
			var form = layui.form,
			layer = layui.layer;
			//赋值
			form.val('dis_form',{
				"id":chance.id,
				"chanceName":chance.chanceName,
				"content":chance.content,
				"createBy":chance.createBy,
				"updateBy":chance.updateBy,
				"createDate":chance.createDate,
				"updateDate":chance.updateDate
			})
			if(chance.type==1){
				form.val('dis_form',{
					"type":"官网流量"
				})
			}
			if(chance.type==2){
				form.val('dis_form',{
					"type":"转介绍"
				})
			}
			if(chance.type==3){
				form.val('dis_form',{
					"type":"客户来访"
				})
			}
			if(chance.type==4){
				form.val('dis_form',{
					"type":"媒体进线"
				})
			}
			//监听提交 
			form.on('submit(dis_form)',function(data){
				console.log(data);
				//console.log(data.field);
				addFormData = data.field;
				dis();
				return false;
			});
		})
	}
	//分配
	function dis(){
		var userId = $("#userId").val();
		var id = $("#id").val();
		$.post("/chance/dis","distribution="+userId+"&id="+id,function(result){
			if(result.status==0){
				layer.msg(result.msg,function(){
					//关闭弹出层
					var index = parent.layer.getFrameIndex(window.name);
					parent.layer.close(index);
				})
			}else{
				layer.msg(result.msg);
			}
		})
	}
	//异步改为同步  async:false;
	//获取用户姓名、id列表
	function getUserId(){
		$.ajax({
			url:"/user/getUserIds",
			type:"post",
			async:false,
			success:function(result){
				if(result.status==0){
					showUserName(result);	
				}else{
					layer.msg(result.msg);
				}
			}
		})
	}
	//展示下拉框
	function showUserName(result){
		var str= "";
		var list = result.data;
		for(i in list){
			if(_this.chance.distribution==list[i].userName){
				str+='<option selected="selected" value="'+list[i].id+'">'+_this.chance.distribution+'</option>';
			}else{
				str+="<option  value="+list[i].id+">"+list[i].userName+"</option>";
			}
		}
		$("#userId").append(str);
	}
	//返回
	function black(){
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	}
	
</script>
